package com.qcby.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qcby.dao.TeachersDao;
import com.qcby.entity.Classes;
import com.qcby.entity.Student;
import com.qcby.entity.Teachers;
import com.qcby.entity.vo.TeachersVo;
import com.qcby.service.TeachersService;
import com.qcby.util.ResultObject;
import com.qcby.util.SystemDescription;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qcby.entity.Tc;
import java.util.ArrayList;
import java.util.List;

/**
 * @author haozeWang
 * @create 2021-08-23 17:46
 */
@Service
public class TeachersServiceImpl implements TeachersService {
    @Autowired
    private TeachersDao teachersDao;
    /*查所有*/
    @Override
    public ResultObject selectAll(TeachersVo teachersVo) {
        Page<Object> page = PageHelper.startPage(teachersVo.getPage(), teachersVo.getLimit());
        if(teachersVo.getName()!=null&&!teachersVo.getName().equals("")){
            teachersVo.setName("%"+teachersVo.getName()+"%");
        }else {
            teachersVo.setName(null);
        }
        if(teachersVo.getTnumber()!=null&&!teachersVo.getTnumber().equals("")){
            teachersVo.setTnumber("%"+teachersVo.getTnumber()+"%");
        }else {
            teachersVo.setTnumber(null);
        }

        List<TeachersVo> teachers = teachersDao.selectAll(teachersVo);
        ResultObject resultObject = new ResultObject("",0,page.getTotal(),teachers);
        return resultObject;
    }
    /*延迟查询老师对应的班级*/
    @Override
    public ResultObject selectById(Integer id) {
        List<TeachersVo> teachersVos=teachersDao.selectJoinById(id);
        /*将同一个老师的全部班级放在一个容器里*/
        TeachersVo teachersVo=new TeachersVo();

        ArrayList<Classes> classes = new ArrayList();

        for (TeachersVo teachervo:teachersVos) {
            classes.add(teachervo.getClasses().get(0));
        }
        teachersVo.setClasses(classes);
        ResultObject resultObject = new ResultObject("",0,1L, teachersVo);
        return resultObject;
    }
    /*删除单个byid*/
    @Override
    public ResultObject delete(Integer id) {
        Teachers teachers = new Teachers();
        teachers.setId(id);
        teachers.setAvailable(SystemDescription.CODECURDFAIL);
        int i = teachersDao.updateByPrimaryKeySelective(teachers);
        teachersDao.deltc(id);
        String msg=i==0?SystemDescription.DELETEFAIL:SystemDescription.DELETEOK;
        ResultObject resultObject = new ResultObject(i, msg);
        return resultObject;
    }
    /*添加教师*/
    @Override
    public ResultObject add(TeachersVo teachersVo) {
        int i = teachersDao.insertSelective(teachersVo);
        int teacherid=teachersVo.getId();
        List<Integer> sel=teachersVo.getSelect();
        Tc tc = new Tc();
        tc.setTeacherid(teacherid);
        for (int j = 0; j <sel.size() ; j++) {
            tc.setClassid(sel.get(j));
            teachersDao.inserttc(tc);
        }
        String msg = i== SystemDescription.CODEFAIL ?SystemDescription.ADDFAIL:SystemDescription.ADDOK;
        ResultObject resultObject = new ResultObject(i, msg);
        return resultObject;
    }
    /*修改学生*/
    @Override
    public ResultObject update(TeachersVo teachersVo) {
        int i = teachersDao.updateByPrimaryKeySelective(teachersVo);
        int teacherid=teachersVo.getId();
        teachersDao.deltc(teacherid);
        List<Integer> sel=teachersVo.getSelect();
        Tc tc = new Tc();
        tc.setTeacherid(teacherid);
        for (int j = 0; j <sel.size() ; j++) {
            tc.setClassid(sel.get(j));
            teachersDao.inserttc(tc);
        }
        String msg = i== SystemDescription.CODEFAIL ?SystemDescription.UPDATEFAIL:SystemDescription.UPDATEOK;
        ResultObject resultObject = new ResultObject(i, msg);
        return resultObject;
    }

    /*批量删除教师*/
    @Override
    public ResultObject deleteDisp(ArrayList<Integer> ids) {
        int flag=SystemDescription.CODECURDOK;
        for (Integer id:ids) {
            ResultObject deleteitem = delete(id);
             if(deleteitem.getCode()==SystemDescription.CODECURDFAIL){
                 flag=SystemDescription.CODECURDFAIL;
             }
        }
        String msg=flag==0?SystemDescription.DELETEDISPFAIL:SystemDescription.DELETEDISPOK;
        ResultObject resultObject = new ResultObject(flag,msg);
        return resultObject;
    }
}
